package org.alex.sample.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

	public static String getMD5Code(String strObj) {
		String resultString = null;
		try {
			
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = new BigInteger(1,md.digest(strObj.getBytes())).toString(16);//用MD5加密密码
			//MD5得到32位字符串开头为“0”不显示时就加上“0”
			if(resultString.length() < 32){
				int len = resultString.length();
				for(int i = 0;i < 32 - len;i++){
					resultString = "0" + resultString;
				}
			}
			//md.digest() 该函数返回值为存放哈希值结果的byte数组
		}catch(NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return resultString;
	}
}
